<?php
// função dias semana conforme turma cadastradas
function getSemana($db,$curso = 0){
    $arr_nrt = array();
    //$query = 'SELECT SQL_CACHE DISTINCT n.NRT_SEM, n.NRT_ID FROM NUMEROTURMA n, CURSO c WHERE n.NRT_STA = 1 and n.NRT_ID = c.CUR_TRM and c.CBS_ID = '.$curso.' ORDER BY n.NRT_SEM ASC';
    $query = 'SELECT SQL_CACHE DISTINCT NRT_SEM, NRT_ID FROM NUMEROTURMA WHERE NRT_STA = 1 ORDER BY NRT_SEM ASC';
    $obj = $db->fetchAll($query);
    foreach($obj as $o){
        $arr_nrt[$o['NRT_SEM']] = $o['NRT_SEM'];
    }
    return $arr_nrt;
}

// função retorna todos os horarios dos turnos
function getTurno($db,$curso = 0){
    $turno = array();
    
    //$query = 'SELECT SQL_CACHE DISTINCT n.NRT_HOR FROM NUMEROTURMA n, CURSO c WHERE n.NRT_STA = 1 and n.NRT_ID = c.CUR_TRM and c.CBS_ID = '.$curso.' ORDER BY n.NRT_HOR ASC';
    $query = 'SELECT SQL_CACHE DISTINCT NRT_HOR FROM NUMEROTURMA WHERE NRT_STA = 1 ORDER BY NRT_HOR ASC';
    $obj = $db->fetchAll($query);
    foreach($obj as $o){
        $hora = (int) substr($o['NRT_HOR'], 0, 2);
        if($hora >= 8 && $hora <= 12) $turno[0] = 'Manhã';
        else if($hora > 12 && $hora <= 19) $turno[1] = 'Tarde';
        else if($hora > 19) $turno[2] = 'Noite';
    }
    
    return $turno;
}

// função retorna horários conforme a semana e o horario do turno
function getHorarios($db,$sem,$turno,$limit = ''){
    
    $where = '';
    if($turno == 0) $where = 'and NRT_HOR >= "08:00:00" and NRT_HOR <= "12:00:00" ';
    else if($turno == 1) $where = 'and NRT_HOR > "12:00:00" and NRT_HOR <= "19:00:00" ';
    else if($turno == 2) $where = 'and NRT_HOR > "19:00:00"';
    
    if($limit > '') $limit = ' LIMIT '.$limit;
        
    $query = 'SELECT SQL_CACHE SUBSTR(NRT_HOR,1,5) as NRT_HOR, NRT_SEM, NRT_ID FROM NUMEROTURMA WHERE NRT_SEM = '.$sem.' '.$where.' and NRT_STA = 1 ORDER BY NRT_HOR ASC'.$limit; 
    $obj = $db->fetchAll($query);
    return $obj; 
}

// função se tiver algum curso cadastrado nesse turno não mostra para os demais
function mostrarHorario($db,$curso = 0,$turno = 0){
    if(!is_array($turno)){
  
        $query= 'SELECT SQL_CACHE NRT_ID FROM NUMEROTURMA WHERE NRT_ID = '.$turno.' and NRT_STA = 1';
        $obj = $db->fetchRow($query);
        if($obj['NRT_ID'] > '0'){
            
            $query= 'SELECT SQL_CACHE CUR_ID,CUR_NMRMAXALU FROM CURSO WHERE CBS_ID = '.$curso.' and CUR_TRM = '.$obj['NRT_ID'].' and CUR_STA = 1';
            $obj = $db->fetchRow($query);
            if($obj['CUR_ID'] > 0){
                // verifica se tem vaga ainda
                $where = 'SELECT count(*) as TOTAL FROM MATRICULA WHERE MAT_STA = 1 AND CUR_ID = '.$obj['CUR_ID'];
                $mat = $db->fetchRow($query);
                $total  = $obj['CUR_NMRMAXALU'] - $mat['TOTAL'];   

                if($total == 0) return FALSE;
                else return TRUE;  
            }else{
                
                $query= 'SELECT SQL_CACHE DISTINCT n.NRT_ID, c.CUR_ID FROM NUMEROTURMA n '
                        . ' LEFT JOIN CURSO c on c.CUR_TRM = n.NRT_ID AND c.CUR_STA = 1 '
                        . ' WHERE n.NRT_ID = '.$turno;
                
                $obj = $db->fetchAll($query);
                foreach($obj as $o){
                    if($o['CUR_ID'] == NULL){
                        return TRUE;
                    }
                }
                return FALSE;
            }
        }else{
            return FALSE;
        }
        
    }
}

function mostrarSemana($db,$sem,$curso = 0){
    
    $query= 'SELECT SQL_CACHE NRT_ID FROM NUMEROTURMA WHERE NRT_SEM = '.$sem.' and NRT_STA = 1';
    $obj = $db->fetchAll($query);
    $list = '';
    foreach($obj as $o) $list .= $o['NRT_ID'].',';
    $list = substr($list, 0, -1);
    
    if($list > '0'){
        
        $query= 'SELECT SQL_CACHE CUR_ID, CUR_NMRMAXALU FROM CURSO WHERE CBS_ID = '.$curso.' and CUR_TRM in('.$list.') and CUR_STA = 1';
        $obj = $db->fetchRow($query);
        if($obj['CUR_ID'] > 0){
            // verifica se tem vaga ainda
            $where = 'SELECT count(*) as TOTAL FROM MATRICULA WHERE MAT_STA = 1 AND CUR_ID = '.$obj['CUR_ID'];
            $mat = $db->fetchRow($query);
            $total  = $obj['CUR_NMRMAXALU'] - $mat['TOTAL'];   

            if($total == 0) return FALSE;
            else return TRUE;
        }else{
            $query= 'SELECT SQL_CACHE n.NRT_ID, c.CUR_ID FROM NUMEROTURMA n '
                    . ' LEFT JOIN CURSO c on c.CUR_TRM = n.NRT_ID AND c.CUR_STA = 1'
                    . ' WHERE n.NRT_ID in('.$list.')';
            
            $obj = $db->fetchAll($query);
            foreach($obj as $o){
                if($o['CUR_ID'] == NULL){
                    return TRUE;
                }
            }
            return FALSE;
        }
    }else{
        return FALSE;
    }
}

?>

<div class="st-cp-pg-cursos">    
    
    <ul>
    <?php foreach ($this->cursos as $key => $cur){ ?>
        <li class="<?php echo 'curso'.($key+1);?>">
            
                <div class="ctn_img ctn_caixa">
                    <?php
                    $img = glob('public/upload/curso/'.$cur['CBS_ID'].'.*');
                    if(isset($img[0])){ ?>    
                    <img src="<?php echo $img[0]; ?>" alt="<?php echo $cur['CBS_NOM'];?>"/>
                    <?php } ?>     
                    <div class="caixa_tl">
                        <div class="st_cima"></div>
                        <div class="tl"><?php echo $cur['CBS_NOM'];?></div>
                    </div>
                </div>
                <p><?php echo $cur['CBS_DES'];?></p> 
                <p><strong>Módulo:</strong> <?php echo $cur['CBS_MOD'];?></p>           
                <p><strong>Objetivos:</strong> <?php echo $cur['CBS_OBJ'];?></p>          
                <p><strong>Estrutura:</strong> <?php echo $cur['CBS_EST'];?></p>        
                <a href="javascript:void(0)" class="registrar" itemid="<?php echo $cur['CBS_ID'];?>">registrar interesse <span>&rsaquo;</span></a>

                <div id="conteudo_interesse<?php echo $cur['CBS_ID'];?>" style="display: none;">
                    <form class="form <?php echo 'curso'.($key+1);?>">
                        <div class="col img ctn_caixa">
                            <img src="<?php echo $img[0]; ?>" alt="<?php echo $cur['CBS_NOM'];?>"/>
                            <div class="caixa_tl">
                                <div class="st_cima"></div>
                                <div class="tl"><?php echo $cur['CBS_NOM'];?></div>
                            </div>
                        </div>
                        <div class="col txt">
                            <h2>registro de interesse</h2>
                            <p><?php echo $cur['CBS_DES'];?></p>
                            <?php 
                                $arr_nrt = getSemana($this->db,$cur['CBS_ID']);
                            ?>
                            <?php
                            $turno = getTurno($this->db,$cur['CBS_ID']);                             
                            ?>
                            <h3>selecione um horário ainda disponível</h3>
                            <table class="horarios">
                                <?php foreach($turno as $key_tur => $tur){ ?>
                                <tr>
                                    <td><strong>&nbsp;</strong></td>
                                    <?php
                                    $sem = Datetools::semana(null,'2');
                                    foreach($sem as $key => $s){
                                        if(array_search($key, $arr_nrt) > 0){
                                    ?>
                                        <td class="cab">
                                            <?php if(mostrarSemana($this->db,$key,$cur['CBS_ID'])){ ?>
                                            <strong><?php echo Datetools::semana($key,'2'); ?></strong> 
                                            <?php }  ?>
                                        </td>
                                    <?php
                                            }
                                        } ?>
                                    </tr>  
                                <?php } ?>
                                <?php foreach($turno as $key_tur => $tur){ ?>
                                <tr>
                                    <td><strong><?php echo $tur; ?></strong></td>
                                    <?php
                                    $sem = Datetools::semana(null,'2');
                                    foreach($sem as $key => $s){
                                        if(array_search($key, $arr_nrt) > 0){
                                    ?>
                                    <td>
                                        <?php foreach(getHorarios($this->db,$key,$key_tur) as $arr){
                                        if(mostrarHorario($this->db, $cur['CBS_ID'], $arr['NRT_ID'])){ ?>
                                        <span class="horario"><?php echo str_replace(':', 'h', $arr['NRT_HOR']); ?>min</span><input type="checkbox" name="horario[]" class="ck_horario" value="<?php echo $arr['NRT_ID'];?>"> 
                                        <?php }  ?>
                                        <?php }  ?>
                                    </td>
                                    <?php } 
                                        } ?>
                                    </tr>  
                                <?php } ?>
                            </table>                        

                            <h3>dados</h3>
                            <div class="cnt-form">
                                <input type="hidden" name="id" value="<?php echo $cur['CBS_ID'];?>" />
                                <table>
                                    <tr>
                                        <td>Nome</td>
                                        <td><input type="text" name="nome" required=""></td>
                                    </tr>
                                    <tr>
                                        <td>E-mail</td>
                                        <td><input type="text" name="email" class="email" required=""></td>
                                    </tr>
                                    <tr>
                                        <td>Telefone</td>
                                        <td><input type="text" name="telefone" class="telefone" required=""></td>
                                    </tr>
                                    <tr>
                                        <td>Cidade</td>
                                        <td><input type="text" name="cidade" required=""></td>
                                    </tr>     
                                </table>
                                <button type="submit">enviar</button>
                            </div>
                        </div>
                    </form>
                </div>
            
        </li>
        
    <?php } ?>
    </ul>
</div>

<a class="fc_popup" href="#popup"></a>
<div id="popup" class="ctn-popup" style="display: none;"></div>

<script type="text/javascript" src="<?php echo $this->urlbase; ?>/public/media/js/jquery-ui-1.10.2.custom/js/jquery-1.9.1.min.js"></script>   
<script type="text/javascript" src="<?php echo $this->urlbase; ?>/public/media/js/validation/dist/jquery.validate.min.js"></script>   
<script type="text/javascript" src="<?php echo $this->urlbase; ?>/public/media/js/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<link rel="stylesheet" type="text/css" href="<?php echo $this->urlbase; ?>/public/media/js/fancybox/source/jquery.fancybox.css?v=2.1.5" media="screen" />
<script type="text/javascript" src="<?php echo $this->urlbase; ?>/public/media/js/jquery.maskedinput.min.js"></script>
<script type="text/javascript" src="<?php echo $this->urlbase; ?>/public/media/js/view/cursos.js?1=8"></script>
    